package com.aspose.cells;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;

public class Encoding {
   protected com.aspose.cells.b.a.g.zd a;
   private Charset c = null;
   private CharsetDecoder d = null;
   private CharsetEncoder e = null;
   // $FF: synthetic field
   static final boolean b = !Encoding.class.desiredAssertionStatus();

   public static Encoding getASCII() {
      return new Encoding(com.aspose.cells.b.a.g.zd.a);
   }

   public static Encoding getUTF7() {
      return new Encoding(com.aspose.cells.b.a.g.zd.b);
   }

   public static Encoding getUTF8() {
      return new Encoding(com.aspose.cells.b.a.g.zd.c);
   }

   public static Encoding getUTF8NoBOM() {
      return new Encoding(com.aspose.cells.b.a.g.zd.d);
   }

   public static Encoding getUnicode() {
      return new Encoding(com.aspose.cells.b.a.g.zd.e);
   }

   public static Encoding getBigEndianUnicode() {
      return new Encoding(com.aspose.cells.b.a.g.zd.f);
   }

   protected Encoding(com.aspose.cells.b.a.g.zd data) {
      this.a = data;
   }

   public static Encoding a(boolean var0, boolean var1) {
      return var0 ? getBigEndianUnicode() : getUnicode();
   }

   public static Encoding getDefault() {
      return getEncoding(Charset.defaultCharset().name());
   }

   public static Encoding b(boolean var0, boolean var1) {
      throw new UnsupportedOperationException("UTF32 Encoding not supported in java version.");
   }

   public static Encoding getEncoding(int codePage) {
      com.aspose.cells.b.a.g.zd[] var1 = com.aspose.cells.b.a.g.zd.a();
      int var2 = var1.length;

      for(int var3 = 0; var3 < var2; ++var3) {
         com.aspose.cells.b.a.g.zd var4 = var1[var3];
         if (var4.S == codePage) {
            return new Encoding(var4);
         }
      }

      return getUTF8();
   }

   public static Encoding getEncoding(String charsetName) {
      com.aspose.cells.a.e.zg.b(charsetName, "charsetName");
      com.aspose.cells.b.a.g.zd[] var1 = com.aspose.cells.b.a.g.zd.a();
      int var2 = var1.length;

      for(int var3 = 0; var3 < var2; ++var3) {
         com.aspose.cells.b.a.g.zd var4 = var1[var3];
         if (charsetName.equalsIgnoreCase(var4.T)) {
            return new Encoding(var4);
         }
      }

      return getUTF8();
   }

   public static Encoding getEncoding(Charset charset) {
      return getEncoding(charset.displayName());
   }

   public String a() {
      return this.a.U;
   }

   public String b() {
      return this.a.T;
   }

   public com.aspose.cells.b.a.g.zb c() {
      return new com.aspose.cells.b.a.g.zb(this.h());
   }

   CharsetDecoder d() {
      if (this.d == null) {
         this.d = this.h().newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
      }

      return this.d;
   }

   public com.aspose.cells.b.a.g.zc e() throws Exception {
      return new com.aspose.cells.b.a.g.zc(this.h());
   }

   CharsetEncoder f() throws Exception {
      if (this.e == null) {
         this.e = this.h().newEncoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
      }

      return this.e;
   }

   public int a(int var1) throws Exception {
      return (int)((float)var1 * this.f().maxBytesPerChar());
   }

   public byte[] g() {
      switch(null.a[this.a.ordinal()]) {
      case 1:
         return new byte[]{-17, -69, -65};
      case 2:
         return new byte[]{-1, -2};
      case 3:
         return new byte[]{-2, -1};
      default:
         return new byte[0];
      }
   }

   public byte[] a(String var1) {
      ByteBuffer var2 = this.h().encode(var1);
      return this.a(var2);
   }

   private byte[] a(ByteBuffer var1) {
      byte[] var2 = var1.array();
      int var3 = var1.remaining();
      if (var3 == var2.length) {
         return var2;
      } else {
         byte[] var4 = new byte[var3];
         System.arraycopy(var2, 0, var4, 0, var3);
         return var4;
      }
   }

   public String a(byte[] var1, int var2, int var3) {
      com.aspose.cells.a.e.za.a(var1, var2, var3);
      if (var3 == 0) {
         return "";
      } else {
         try {
            CharBuffer var4 = this.d().decode(ByteBuffer.wrap(var1, var2, var3));
            return var4.toString();
         } catch (Exception var5) {
            this.a(var5);
            return "";
         }
      }
   }

   private void a(Exception var1) {
      throw new IllegalStateException("Invalid encoding: " + var1.getMessage(), var1);
   }

   public String a(byte[] var1) {
      try {
         CharBuffer var2 = this.d().decode(ByteBuffer.wrap(var1));
         return var2.toString();
      } catch (Exception var3) {
         this.a(var3);
         return "";
      }
   }

   public Charset h() {
      if (this.c != null) {
         return this.c;
      } else {
         try {
            this.c = Charset.forName(this.a.T);
         } catch (IllegalArgumentException var2) {
            throw new IllegalStateException(var2);
         }

         return this.c;
      }
   }

   public boolean equals(Object o) {
      return o instanceof Encoding && this.equals((Encoding)o);
   }

   public boolean equals(Encoding other) {
      return this.a == other.a;
   }

   public int i() {
      return this.a.S;
   }

   public char[] getChars(byte[] bytes, int index, int count) {
      com.aspose.cells.a.e.za.a(bytes, index, count);
      CharsetDecoder var4 = this.d();
      int var5 = (int)(var4.maxCharsPerByte() * (float)count);
      CharBuffer var6 = CharBuffer.allocate(var5);
      ByteBuffer var7 = ByteBuffer.wrap(bytes, index, count);
      var4.reset();
      var4.decode(var7, var6, false);
      int var9 = var6.position();
      char[] var10 = new char[var9];
      var6.rewind();
      var6.get(var10);
      return var10;
   }
}
